package com.example.lending.View;


import android.content.SharedPreferences;
import android.net.NetworkInfo;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.SavedStateViewModelFactory;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;

import com.example.lending.Model.Book;
import com.example.lending.Model.MyData;
import com.example.lending.Model.User;
import com.example.lending.R;
import com.example.lending.ViewModel.UserViewModel;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class LoginFragment extends Fragment {


    public LoginFragment() {
        // Required empty public constructor
    }
    ImageView imageView1,imageView2;
    EditText et_username,et_password;
    TextView tv_register,tv_forget;
    Button btn_login;
    SharedPreferences sp;
    MyData myData;
    LiveData<List<User>> liveDataUser;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_login, container, false);
    }
    int j=0;
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        imageView1=getView().findViewById(R.id.imageView);
        imageView2=getView().findViewById(R.id.imageView2);
        et_username=getView().findViewById(R.id.et_username);
        et_password=getView().findViewById(R.id.et_password);
        tv_register=getView().findViewById(R.id.tv_register);
        tv_forget=getView().findViewById(R.id.tv_forget);
        btn_login=getView().findViewById(R.id.btn_login);
        myData=new MyData(getActivity());
        if(myData.load()==1){
            myData.saveNo();
        }
        sp=getActivity().getSharedPreferences("user",getActivity().MODE_PRIVATE);
        final UserViewModel userViewModel=ViewModelProviders.of(getActivity(),new SavedStateViewModelFactory(getActivity().getApplication(),getActivity())).get(UserViewModel.class);
        if(userViewModel.getIsLogin()==1){//如果登陆过，就是直接进去了
            NavController controller=Navigation.findNavController(getActivity(),R.id.fragment4);
            controller.navigate(R.id.action_loginFragment2_to_mainActivity);
        }
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(et_username.getText()) || TextUtils.isEmpty(et_password.getText())) {
                    Toast.makeText(getActivity(), "请输入您的账号和密码", Toast.LENGTH_SHORT).show();
                } else {
                    //这里可以补充一个网络判断
                    //liveDataUser=userViewModel.getAllUserLive();
                    liveDataUser = userViewModel.findUserWithPattern(et_username.getText().toString());
                    liveDataUser.removeObservers(getViewLifecycleOwner());
                    liveDataUser.observe(getViewLifecycleOwner(), new Observer<List<User>>() {
                        @Override
                        public void onChanged(List<User> users) {
                            if(liveDataUser.getValue().size()==0){
                                //System.out.println("88888888888888"+":"+liveDataUser.getValue().size());
                                Toast.makeText(getActivity(), "账号或密码错误，请重新输入", Toast.LENGTH_SHORT).show();
                            }else {
                                for (int i = 0; i < users.size(); i++) {
                                    if (users.get(i).getPassword().equals(et_password.getText().toString())) {
                                        Toast.makeText(getActivity(), "登陆成功", Toast.LENGTH_SHORT).show();
                                        j = 1;
                                        sp.edit().putString("username", et_username.getText().toString()).putInt("usertype", users.get(i).getType()).commit();
                                        //保存一下，要让所有的fragment知道这里登陆的账号是管理员的还是普通用户的
                                        myData.saveYes();
                                        userViewModel.setIsLogin(1);//声明账号记住
                                        NavController controller = Navigation.findNavController(getActivity(), R.id.fragment4);
                                        controller.navigate(R.id.action_loginFragment2_to_mainActivity);
                                        break;
                                    } else {
                                        System.out.println("99999999999999");
                                        Toast.makeText(getActivity(), "账号或密码错误，请重新输入", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            }
                        }
                    });
                }
            }
        });

        tv_register.setOnClickListener(new View.OnClickListener() {//跳转到注册界面
            @Override
            public void onClick(View v) {
                NavController controller =Navigation.findNavController(v);
                controller.navigate(R.id.action_loginFragment2_to_regFragment);
            }
        });
        tv_forget.setOnClickListener(new View.OnClickListener() {//跳转到忘记密码界面
            @Override
            public void onClick(View v) {
                NavController controller=Navigation.findNavController(v);
                controller.navigate(R.id.action_loginFragment2_to_fogetFragment);
            }
        });


    }
}
